var device=request("from");
var phone=request("phone");
var couch_phone=request("couch_phone");

//时间戳转换成时分秒时间
/*
 call:var stamp=timestamp*1000;
 eg:(new Date(stamp)).Format("yyyy-MM-dd hh:mm:ss")      ==> 2016-05-31 08:09:40
 */
Date.prototype.Format = function(fmt) { //author: meizz
	var o = {
		"M+": this.getMonth() + 1, //月份
		"d+": this.getDate(), //日
		"h+": this.getHours(), //小时
		"m+": this.getMinutes(), //分
		"s+": this.getSeconds(), //秒
		"q+": Math.floor((this.getMonth() + 3) / 3), //季度
		"S": this.getMilliseconds() //毫秒
	};
	if (/(y+)/.test(fmt))
		fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
	for (var k in o)
		if (new RegExp("(" + k + ")").test(fmt))
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
	return fmt;
}

$(function(){
	//报名咨询跳转
	$("#sign_up").click(function(){
		location.href="sign_up.html?phone="+phone+"&couch_phone="+couch_phone+"&from="+device;
	})
	//编辑名片跳转
	$("#card_edit").click(function(){
		location.href="card.html?phone="+phone+"&from="+device;
	})
	//教练入驻
	$(".register_prompt").click(function(){
		if(device=="app"){
			location.href="qicheng://to_be_coach"
		}
		else{
			location.href="instructor_enter.html"
		}
	})
	//排行榜按钮
	$(".ranking .more").click(function(){
		location.href="qicheng://ranking"
	})
	
   //教学环境显示大图
   var swiper2;
   var is_first_show=true;
    $(".swiper-container").on("click",".swiper-slide",function(){
    	if(is_first_show){
    		is_first_show=false;
    		$(".big_img").show();
	        swiper2 = new Swiper('.swiper-container2', {
		        pagination: '.swiper-pagination',
		        paginationClickable: true,
		        initialSlide :$(this).index()
		    });
    	}
    	else{
    		swiper2.slideTo($(this).index())
    		$(".big_img").show();
    	}
    })
    $(".big_img .close").click(function(){
    	$(".big_img").hide();
    })
    //设置关注接口
    $(".concern").click(function(){
    	if($(this).attr("is_follow")=="0"){
    		set_follow("1")
    	}
    	else if($(this).attr("is_follow")=="1"){
    		set_follow("0")
    	}
    })
    function set_follow(set){
    	$.ajax({
		    url: DATA_URL+"follow",
		    data: "phone="+phone+"&couch_phone="+couch_phone+"&set="+set,
		    type: "post",
		    dataType: 'json',
		    timeout: 5000,
		    success:function (data) {
		    	if(data.code=="0"){
					if(set=="0"){
						$(".concern").attr("is_follow","0");
						$(".evaluate_btn").hide();
						$(".instructor_info_top .concern").text("关注")
						$(".instructor_info_top .concern").css({
							"border":"#43abeb solid 1px",
							"background":"#fff",
							"color":"#43abeb"
						})
						location.href="qicheng://cancel_student"
					}
					else if(set=="1"){
						$(".concern").attr("is_follow","1");
						if($(".evaluate_btn").attr("is_evaluate")==0){
							$(".evaluate_btn").show();
						}
						else if($(".evaluate_btn").attr("is_evaluate")==1){
							$(".evaluate_btn").hide();
						}
						$(".instructor_info_top .concern").text("已关注")
						$(".instructor_info_top .concern").css({
							"border":"0",
							"background":"#43abeb",
							"color":"#fff"
						})
						location.href="qicheng://attach_student"
					}
					$("#prompt").text(data.msg);
		            $(".prompt").fadeIn(300,function(){
		                setTimeout(function(){$(".prompt").fadeOut(300)},3000)
		            })
		    	}
				else{
					$("#prompt").text(data.msg);
		            $(".prompt").fadeIn(300,function(){
		                setTimeout(function(){$(".prompt").fadeOut(300)},3000)
		            })
				}
		    },
		    error:function () {
		    	$("#prompt").text("您的网络不给力哦~");
		        $(".prompt").fadeIn(300,function(){
		            setTimeout(function(){$(".prompt").fadeOut(300)},3000)
		        })
		    }
		});
    }
    //获取信息
    $.ajax({
	    url: DATA_URL+"getcouchdetail",
	    data: "phone="+phone+"&couch_phone="+couch_phone,
	    type: "post",
	    dataType: 'json',
	    timeout: 5000,
	    success:function (data) {
	    	if(data.code=="0"){
				$(".instructor_info_top .photo").css("background-image","url("+data.info.avatar+")");//头像
				$(".instructor_info_top .name").text(data.info.nick_name)//昵称
				$(".instructor_info_top .remarks span:nth-child(1)").text(data.info.years)//驾龄
				$(".instructor_info_top .remarks span:nth-child(2)").text(data.info.followers)//学员数
				$(".instructor_info_top .remarks span:nth-child(3)").text(data.info.score)//得分
				//认证图标
				$.each(data.info.auth_status,function(index,val){
					$(".instructor_info_top .certificates").append("<div>"+val+"<span></span></div>")
				})
				//关注状态
				$(".instructor_info_top .concern").attr("is_follow",data.info.is_follow)
				$(".evaluate_btn").attr("is_evaluate",data.info.is_evaluate)//评价状态
				if(data.info.is_follow==0){
					$(".evaluate_btn").hide()
					$(".instructor_info_top .concern").text("关注")
					$(".instructor_info_top .concern").css({
						"border":"#43abeb solid 1px",
						"background":"#fff",
						"color":"#43abeb"
					})
				}
				else if(data.info.is_follow==1){
					if(data.info.is_evaluate==0){
						$(".evaluate_btn").show()
					}
					else if(data.info.is_evaluate==1){
						$(".evaluate_btn").hide()
					}
					$(".instructor_info_top .concern").text("已关注")
					$(".instructor_info_top .concern").css({
						"border":"0",
						"background":"#43abeb",
						"color":"#fff"
					})
				}
				if(phone==couch_phone||device!="app"){
					$(".evaluate_btn").hide()
				}
				//评价按钮
				$(".evaluate_btn").click(function(){
					location.href="qicheng://evaluate?couch_phone="+couch_phone+"&photo="+data.info.avatar;
				})
				
				$(".ranking .title span").text(data.info.rank);//排名
				//驾校情况
				$("#school").text(data.info.school);
				$("#phone").text(data.info.tel);
				$("#phone").attr("href","tel:"+data.info.tel);
				$("#adress").text(data.info.addr.area_str);
				//驾校地址跳转
				$("#adress").click(function(){
					location.href="map.html?longitude="+data.info.addr.longitude+"&latitude="+data.info.addr.latitude+"&area_str="+data.info.addr.area_str;
				})
				//班型信息
				var course=""
				$.each(data.info.class_info, function(index,val) {
					course+='<div class="course">\
								<div class="course_info">\
									<div class="name"><span>'+val.class_name+'</span>&nbsp;&nbsp;<span>'+val.car_type+'</span></div>\
									<div class="price">￥'+val.price+'</div>\
									<div class="clear"></div>\
								</div>\
								<div class="course_introduce">'+val.desc+'</div>\
							</div>'	
				});
				$("#course").html(course);
				//教学环境
				var pics=""
				$.each(data.info.addr.pics, function(index,val) {
					pics+='<div class="swiper-slide" style="background-image: url('+val+');"></div>'
				});
				$(".swiper-container .swiper-wrapper").html(pics);
				$(".swiper-container2 .swiper-wrapper").html(pics);
				//教学环境图片滚动
				var swiper = new Swiper('.swiper-container', {
			        slidesPerView: 3,
			        spaceBetween: 10
			    });
			    //特色服务
			   	var points=""
			    $.each(data.info.points,function(index,val){
			    	points+="<span>"+val+"</span>"
			    })
			    $(".service_tag").html(points)
			    //评价列表
			    var evaluate_list=""
			    $.each(data.info.evaluate, function(index,val) {
			    	var stars=""
			    	for(var i=0;i<5;i++){
			    		if(i<val.stars){
			    			stars+='<span class="active"></span>'
			    		}
			    		else{
			    			stars+='<span></span>'
			    		}
			    	}
			    	evaluate_list+='<li>\
										<div class="photo" style="background-image: url('+val.avatar+');"></div>\
										<div class="text">\
											<div class="text_t">\
												<div class="name">'+val.nick_name+'</div>\
												<div class="stars">'+stars+'</div>\
												<div class="num">'+val.stars+'星</div>\
												<div class="date">'+(new Date(val.add_time * 1000)).Format("MM-dd hh:mm")+'</div>\
											</div>\
											<div class="clear"></div>\
											<div class="text_b">'+val.content+'</div>\
										</div>\
									</li>'
			    });
			    $(".evaluate_list").html(evaluate_list)
	    	}
			else{
				$("#prompt").text(data.msg);
	            $(".prompt").fadeIn(300,function(){
	                setTimeout(function(){$(".prompt").fadeOut(300)},3000)
	            })
			}
	    },
	    error:function () {
	    	$("#prompt").text("您的网络不给力哦~");
	        $(".prompt").fadeIn(300,function(){
	            setTimeout(function(){$(".prompt").fadeOut(300)},3000)
	        })
	    }
	});
	
	//判断是不是教练自己打开自己的页面
	if(phone==couch_phone){
		$(".register_prompt").hide()
		$(".concern").hide()
		$(".evaluate_btn").hide()
		$("#sign_up").hide()
		$("#card_edit").show()
	}
	//是否在微信打开
	if(device!="app"){
		$(".concern").hide()
		$(".ranking").hide()
	}
})
